clc
clear
currentDir = pwd; % 获取当前目录路径
parentDir = fileparts(currentDir); % 获取当前目录的上一级目录路径
% grandParentDir = fileparts(parentDir); % 获取当前目录的上两级目录路径
% greatGrandParentDir = fileparts(grandParentDir); % 获取当前目录的上三级目录路径

%指定需要查找的文件夹
subdir = 'DATA';
%指定需要查找的文件名
filename = '413168000(NoRepeated).mat';%413168000.mat'; %matlab中包含的是剔除
%构造需要查找的文件的完整路径
full_path = fullfile(parentDir, subdir, filename);

load( full_path)  %此处使用的数据是原始org数据经过后面的[]处理后得到的数据

%% 数据预处理
data(data.Speed<1,:)=[];%锚泊状态不处理
AISData=data;

% 设置空间变化阈值
deltaLonThresh = 0.01; % 经度变化阈值
deltaLatThresh = 0.01; % 纬度变化阈值
deltaCourseThresh = 10; % 航向变化阈值
deltaSpeedThresh = 5; % 速度变化阈值

% 初始化异常点标记数组
isOutlier = false(size(AISData.Course));

% 遍历轨迹点，检测异常值
for i = 2:length(AISData.Time)
    if abs(AISData.Lon(i) - AISData.Lon(i-1)) > deltaLonThresh || ...
       abs(AISData.Lat(i) - AISData.Lat(i-1)) > deltaLatThresh || ...
       abs(AISData.Course(i) - AISData.Course(i-1)) > deltaCourseThresh || ...
       abs(AISData.Speed(i) - AISData.Speed(i-1)) > deltaSpeedThresh
        isOutlier(i) = true;
    end
end

% 剔除异常点
cleanedAISData = AISData(~isOutlier, :);

%计算偏转角标准差,
degrees_org = calculate_track_degree(table2array(AISData(:,{'Lon','Lat'})));
degrees_deleted = calculate_track_degree(table2array(cleanedAISData(:,{'Lon','Lat'})));

mean_org=mean(degrees_org);
mean_deleted=mean(degrees_deleted);
std_org=std(degrees_org);
std_deleted = std(degrees_deleted);